package com.zhiyou100.loans.dao.impl;

import java.sql.ResultSet;

import com.zhiyou100.loans.dao.UserDao;
import com.zhiyou100.loans.model.User;
import com.zhiyou100.loans.util.JDBCUtil;

public class UserDaoImpl implements UserDao {
	JDBCUtil util = new JDBCUtil();
	
	@Override
	public int insert(User user) {
		String sql="insert into user(username,password,addr,realname,registertime,idnumber,phonenumber,email) values(?,?,?,?,?,?,?,?)";
		Object[] obj={user.getUsername(),user.getPassword(),user.getAddr(),user.getRealname(),user.getRegistertime(),user.getIdnumber(),user.getPhonenumber(),user.getEmail()};
		return util.update(sql, obj);
	}

	@Override
	public ResultSet select(User user) {
		String sql = "select id,username,password,addr,idnumber,phonenumber,email,realname,registertime from user where username=? and password=?";
		Object[] objs = {user.getUsername(),user.getPassword()};
		return util.query(sql, objs);
	}

	@Override
	public ResultSet balanceSelect(int parseInt) {
		String sql="select  id,userid,money from balance where userid=?";
		Object[] objs={parseInt};
		return util.query(sql, objs);
	}

	@Override
	public int balanceInsert(int userid) {
		String sql="insert into balance(userid,money) values(?,0)";
		Object[] objs={userid};
		return util.update(sql, objs);
	}

	@Override
	public ResultSet select(String username) {
		String sql="select id from user where username=?";
		Object[] objs={username};
		return util.query(sql, objs);
	}

	@Override
	public int updateBalance(double parseDouble,int userid) {
		String sql="update balance set money=money+? where userid=?";
		Object[] objs={parseDouble,userid};
		return util.update(sql, objs);
	}

	@Override
	public ResultSet selectBalanceByUserid(int parseInt) {
		String sql = "select money from balance where userid=?";
		Object[] objs={parseInt};
		return util.query(sql, objs);
	}


}
